package com.bjbn.controller;

import com.bjbn.service.FzLocation1Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/fz-location-1")
@Slf4j
public class FzLocation1Controller {

    @Autowired
    private FzLocation1Service fzLocation1Service;

    @PostMapping("/start")
    public ResponseEntity<Map<String, Object>> startFzLocation1ContinuousProcessing() {
        Map<String, Object> response = new HashMap<>();
        try {
            fzLocation1Service.startFzLocation1ContinuousProcessing();
            response.put("success", true);
            response.put("message", "FZ-Location-1 continuous processing started successfully");
            response.put("totalFiles", fzLocation1Service.getFzLocation1LoadedFiles().size());
            response.put("totalRecords", fzLocation1Service.getFzLocation1TotalRecords());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Error starting FZ-Location-1 continuous processing: {}", e.getMessage(), e);
            response.put("success", false);
            response.put("message", "Failed to start FZ-Location-1 continuous processing: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @PostMapping("/stop")
    public ResponseEntity<Map<String, Object>> stopFzLocation1ContinuousProcessing() {
        Map<String, Object> response = new HashMap<>();
        try {
            fzLocation1Service.stopFzLocation1ContinuousProcessing();
            response.put("success", true);
            response.put("message", "FZ-Location-1 continuous processing stopped successfully");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Error stopping FZ-Location-1 continuous processing: {}", e.getMessage(), e);
            response.put("success", false);
            response.put("message", "Failed to stop FZ-Location-1 continuous processing: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @PostMapping("/reload")
    public ResponseEntity<Map<String, Object>> reloadFzLocation1Data() {
        Map<String, Object> response = new HashMap<>();
        try {
            fzLocation1Service.reloadFzLocation1Data();
            response.put("success", true);
            response.put("message", "FZ-Location-1 data reloaded successfully");
            response.put("totalFiles", fzLocation1Service.getFzLocation1LoadedFiles().size());
            response.put("totalRecords", fzLocation1Service.getFzLocation1TotalRecords());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("Error reloading FZ-Location-1 data: {}", e.getMessage(), e);
            response.put("success", false);
            response.put("message", "Failed to reload FZ-Location-1 data: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getFzLocation1Status() {
        Map<String, Object> status = new HashMap<>();
        status.put("isRunning", fzLocation1Service.isFzLocation1Running());
        status.put("totalRecords", fzLocation1Service.getFzLocation1TotalRecords());
        status.put("totalFiles", fzLocation1Service.getFzLocation1LoadedFiles().size());
        return ResponseEntity.ok(status);
    }

    @GetMapping("/status/detailed")
    public ResponseEntity<Map<String, Object>> getFzLocation1DetailedStatus() {
        return ResponseEntity.ok(fzLocation1Service.getFzLocation1DetailedStatus());
    }

    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> fzLocation1HealthCheck() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("service", "fz-location-1-data-processor");
        health.put("isRunning", fzLocation1Service.isFzLocation1Running());
        health.put("totalRecords", fzLocation1Service.getFzLocation1TotalRecords());
        health.put("totalFiles", fzLocation1Service.getFzLocation1LoadedFiles().size());
        return ResponseEntity.ok(health);
    }
}

